const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        // lat:'',
        // lon:'',
        user_privacy_agreement_des:'',
        user_privacy_agreement_des:'',
        agreed:true,
        allshow:true,
        avatarsUrl:'',
        mobile: '',
        code: '',
        is_send: false,
        time: 60,
        wxcode: '',
        avatar: '',
        nickname: '',
        sex: '',
        inv_code:'',
        inv_code_img:''
    },

    setMobile: function(e){
        this.setData({
            mobile: e.detail.value
        })
    },

    setCode: function(e){
        this.setData({
            code: e.detail.value
        })
    },

    send: function(){
        if(this.data.is_send) return false;
        if(!this.data.mobile){
            wx.showToast({
                title: '请输入手机号',
                icon: 'none'
            });
            return false;
        }
        app.ajax({
            url: '/api/index/code',
            data: {
                mobile: this.data.mobile
            },
            success: res => {
                if(res.code == 1){
                    var time = 60, timer;
                    this.setData({
                        is_send: true
                    })
                    wx.showToast({
                        title: res.msg,
                        icon: 'none'
                    })
                    timer = setInterval(() => {
                        time--;
                        this.setData({
                            time: time
                        })
                        if(time <= 0){
                            clearInterval(timer);
                            this.setData({
                                is_send: false
                            })
                        }
                    }, 1000);
                }else{
                    wx.showToast({
                        title: res.msg,
                        icon: 'none'
                    })
                }
            }
        }, 1);
    },

    loginMobile: function (e) {
        console.log(e);
        if(!this.data.mobile){
            wx.showToast({
                title: '请输入手机号',
                icon: 'none'
            });
            return false;
        }
        if(!this.data.code){
            wx.showToast({
                title: '请输入验证码',
                icon: 'none'
            });
            return false;
        }
        app.ajax({
            url: '/api/user/loginMobile',
            data: {
                mobile: this.data.mobile,
                code: this.data.code,
                avatar: this.data.avatar,
                nickname: this.data.nickname,
                sex: this.data.sex
            },
            success: res => {
                if(res.code == 1){
                    wx.navigateBack({
                        detal: 1
                    })
                }else{
                    wx.showToast({
                        title: res.msg,
                        icon: 'none'
                    })
                }
            }
        }, 1)
    },
    userinfo: function () {
        app.ajax({
            url: '/api/user/userinfo',
            success: res => {
                if (res.code == 1) {
                    wx.setStorageSync('user', res.data);
                    var is_vip = false
                    if(res.data.vip == 0)   is_vip = false
                    else  is_vip = true
                    this.setData({
                        user: res.data,
                        // no_vip: is_vip
                        no_vip: true
                        
                    })
                }
            }
        })
    },
 checkAgreement(allshow) {
        if (!allshow) {
          wx.showToast({
            title: '请勾选隐私协议',
            icon: 'none'
          });
          return false;
        }
        return true;
      },
    loginWx: function (e) {
            // 判断是否勾选隐私协议
      // 判断是否勾选隐私协议
        // if (!this.data.allshow) {
        //     wx.showToast({
        //     title: '请勾选隐私协议',
        //     icon: 'none'
        //     });
        //     return;
        // }
        //   // 判断是否勾选隐私协议
        // if (!this.checkAgreement(this.data.allshow)) {
        //     return;
        // }
       console.log(e.detail.encryptedData,'---e.detail.iv')
          let that=this
        // 获取手机号
        if(!e.detail.iv){
            wx.showToast({
                title: '未获取到手机号',
                icon: 'none'
            });
   
            setTimeout(()=>{
                wx.navigateTo({
                    url: '/pages/index/index',
                  })
            },1500)
            return false;
        }
          
        wx.login({
            success: res => {
                if (res.code) {
                    wx.getUserInfo({
                      success: function (res) {
                        that.setData({
                            avatar:res.userInfo.avatarUrl,
                            nickname:res.userInfo.nickName,
                          })
                          wx.setStorageSync('userinfo', res.userInfo);
                        console.log(res,'用户图像'); // 用户头像链接
                      }
                    })
                  } 
                app.ajax({
                    url: '/api/index/login',
                    data: {
                        code: res.code,
                        iv:e.detail.iv,
                        encryptedData:e.detail.encryptedData
                    },
                    success: res => {
                 
                        if (res.code == 1) {
                         
                            var user = wx.getStorageSync('user');
                            wx.setStorageSync('token', res.data.token);
                            wx.setStorageSync('deposit', res.data.deposit);
                            wx.setStorageSync('money', res.data.money);
                            if(!user || user.id != res.data.id) wx.setStorageSync('user', res.data);
                            console.log('执行了方法/api/index/login')
                        
                            let  inv_code=wx.getStorageSync('inv_code')|| '';
                            
                            console.log(typeof(wx.getStorageSync('inv_code')),'inv_code----')
                            if(inv_code==undefined||inv_code=='undefined'){
                                inv_code=''
                                app.ajax({
                                    url: '/api/user/loginWxa',
                                    data: {
                                        inv_code:inv_code,
                                        code: this.data.wxcode,
                                        iv: e.detail.iv,
                                        encryptedData: e.detail.encryptedData,
                                        avatar: this.data.avatar,
                                        nickname: this.data.nickname,
                                        sex: this.data.gender
                                    },
                                    success: res => {
                                        if(res.code == 1){
                                          
                                            wx.login({
                                              success: (res) => {
                                                console.log('执行了方法/api/user/loginWxa')
                                              },
                                            })
                                           
                                            // wx.showToast({
                                            //     title: res.msg,
                                            //     icon:'none'
                                            //   })
                                            setTimeout(()=>{
                                             
                                                // wx.navigateBack({
                                                //     detal: 1
                                                // })
                                             
                                                wx.navigateTo({
                                                  url: '/pages/index/index',
                                                })
                                            },1000)
                                          
                                        }else{
                                            wx.showToast({
                                                title: res.msg,
                                                icon: 'none'
                                            })
                                            wx.login({
                        
                                                success: res => {
                                                    this.setData({
                                                        wxcode: res.code
                                                    });
                                                }
                                            });
                                        }
                                    }
                                }, 1)
                            }else{
                                app.ajax({
                                    url: '/api/user/loginWxa',
                                    data: {
                                        inv_code:inv_code,
                                        code: this.data.wxcode,
                                        iv: e.detail.iv,
                                        encryptedData: e.detail.encryptedData,
                                        avatar: this.data.avatar,
                                        nickname: this.data.nickname,
                                        sex: this.data.gender
                                    },
                                    success: res => {
                                        if(res.code == 1){
                                          
                                            wx.login({
                                              success: (res) => {
                                                console.log('执行了方法/api/user/loginWxa')
                                              },
                                            })
                                           
                                            // wx.showToast({
                                            //     title: res.msg,
                                            //     icon:'none'
                                            //   })
                                            setTimeout(()=>{
                                             
                                                // wx.navigateBack({
                                                //     detal: 1
                                                // })
                                             
                                                wx.navigateTo({
                                                  url: '/pages/index/index',
                                                })
                                            },1000)
                                          
                                        }else{
                                            wx.showToast({
                                                title: res.msg,
                                                icon: 'none'
                                            })
                                            wx.login({
                        
                                                success: res => {
                                                    this.setData({
                                                        wxcode: res.code
                                                    });
                                                }
                                            });
                                        }
                                    }
                                }, 1)
                            }
             
                           
                   
                        }
                    }
                });
            }
        });

        
      
    },

    goHome: function(){
        wx.redirectTo({
            url: '/pages/index/index',
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        wx.setStorageSync('allflag',true)
        // 下面
        var userinfo = wx.getStorageSync('userinfo');
        console.log(userinfo)
        if(userinfo){
            this.setData({
                avatar: userinfo.avatarUrl,
                nickname: userinfo.nickName,
                gender: userinfo.gender
            })
        }
        this.sketchhaderl()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },
   
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
      console.log("onShow");
        wx.login({
            success: res => {
                this.setData({
                    wxcode: res.code
                });
            }
        });
        wx.setStorageSync('allflag',true)
        // this.loginWx()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {
        wx.showLoading({
            title: '加载中...'
          });
          // 模拟异步请求最新数据
          setTimeout(() => {
            // 数据获取完成后隐藏加载提示
            wx.hideLoading();
            // 停止当前页面下拉刷新
            wx.stopPullDownRefresh();
          }, 1000);
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    },
    shoujihader(){
        app.ajax({
            url: '/api/user/getInvCode',
            success: res => {
                if (res.code == 1) {
                    this.setData({
                        inv_code:res.data.inv_code,
                        inv_code_img:res.data.inv_code_img
                    })
                    wx.setStorageSync('inv_code', res.data.inv_code);
                }
            }
        })

    },
    handleRadioChange(){

        this.setData({
            allshow:!allshow
        })
    },
    useragreement(){//用户协议
      wx.navigateTo({
        url: '/pages/useragreement/useragreement',
      })
    },
    sketchhaderl(){
        app.ajax({
            url: '/api/login/getUserPrivacyAgreement',
            success: res => {
                if (res.code == 1) {
                    this.setData({
                        user_privacy_agreement_des:res.data.user_privacy_agreement_des,
                        user_privacy_agreement_title:res.data.user_privacy_agreement_title
                    })
                  wx.setStorageSync('user_privacy_agreement_des',res.data.user_privacy_agreement_des )   
                 
                }
            }
        })
      },
       // 用户隐私协议
    agreePrivacy: function() {
      
        // 用户点击同意按钮后的逻辑处理
       // 将同意状态保存到本地存储，并更新数据
    //    wx.setStorageSync('agreedPrivacy', true);
       this.setData({
         agreed: false,
         allshow:true
       });
    //    this.getLocation()
    
         },
         denyPrivacy: function() {
            this.setData({
              agreed:!this.data.agreed 
            })
            // wx.showToast({  
            //   title: '定位失败',  
            //   icon: 'none',  
            //   duration: 2000  
            // });  
        },
        Protectinghaderl:function(){
            wx.navigateTo({
                url: '/pages/useragreement/useragreement',
              })

        },
          // 处理勾选隐私协议的事件
    onCheckboxChange: function (e) {
        let value = e.detail.value;
        this.setData({
        allshow: value.length > 0
        });
    },
        // 获取定位
        // getLocation: function() {
        //     wx.getLocation({
        //         type: 'gcj02',
        //         success: res => {
        //             this.setData({
        //                 lat: res.latitude,
        //                 lon: res.longitude,
        //             });
        //             wx.setStorageSync('location', {
        //                 lat: res.latitude,
        //                 lon: res.longitude
        //             });
        //             var city = wx.getStorageSync('city');
        //             if (city) {
        //                 this.setData({
        //                     city: city
        //                 });
        //                 this.getStores();
        //             } else {
        //                 this.getCity();
        //             }
        //         },
        //         fail:err=>{
        //             // showModal showToast
        //             wx.showToast({  
        //                 title: '定位失败',  
        //                 icon: 'none',  
        //                 duration: 2000  
        //               });  
        //         // wx.removeStorageSync('key')
        //         }
        //     })
        // },
        // getUserProfile(){
        //     wx.getUserProfile({
        //          desc: '用于完善会员资料', //这个也很重要
        //          success: (res) => {
        //            console.log(res.userInfo,'测试登录页面res.userInfo')
        //          }
        //        })
        //    }
})